package com.xcjh.module_ws.websocket

import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.View.OnClickListener
import android.widget.ScrollView
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.gson.Gson
import com.xcjh.base_lib.Constants
import com.xcjh.base_lib.base.BaseViewModel
import com.xcjh.base_lib.base.activity.BaseVmDbActivity
import com.xcjh.module_ws.databinding.WsActivity2MainBinding
import com.xcjh.module_ws.websocket.bean.ChatMsgBean
import com.xcjh.module_ws.websocket.bean.SendChatMsgBean
import com.xcjh.module_ws.websocket.listener.C2CListener
import com.xcjh.module_ws.websocket.listener.WsListener

@Route(path = Constants.Router.Ws.A_Ws_MAIN2)
class Ws2MainActivity : BaseVmDbActivity<BaseViewModel, WsActivity2MainBinding>(), OnClickListener {
    override fun initView(savedInstanceState: Bundle?) {

        mDatabind.btnSend.setOnClickListener(this)
        MyWsManager.getInstance(this)?.setWsListener(javaClass.name,object : WsListener {
            override fun onReceive(msg: String) {
                mDatabind.tvMsg.text = mDatabind.tvMsg.text.toString() + "\n\n" + msg
                mDatabind.tvMsg.postDelayed({
                    mDatabind.scrollView.fullScroll(ScrollView.FOCUS_DOWN)
                }, 200)

            }
        })
        MyWsManager.getInstance(this)?.setC2CListener(object : C2CListener {
            override fun onC2CReceive(chat: ChatMsgBean) {
                Log.e("===============", "onC2CReceive: ===" + chat.content)
               // myToast(chat.content)
            }
        })
        Log.e("====", "initView: ===="+javaClass.name )
    }

    private var exitTime: Long = 0
    override fun showLoading(message: String) {

    }

    override fun onDestroy() {
        super.onDestroy()
        MyWsManager.getInstance(this)?.removeWsListener(javaClass.name)
    }
    override fun dismissLoading() {

    }

    override fun createObserver() {

    }

    override fun onClick(v: View?) {
        when (v) {
            mDatabind.btnSend -> {
                MyWsManager.getInstance(this)?.sendMessage(
                    Gson().toJson(
                        SendChatMsgBean(2, 0, 11,
                            "1689193696521482240",
                            "1689193696521482240",
                            to = "1692360254443462656",
                            content = mDatabind.edMsg.text.toString(),
                            createTime = System.currentTimeMillis()
                        )
                    )
                )
            }

        }
    }



}